<?php
/**
 * Created by JetBrains PhpStorm.
 * User: GMC
 * Date: 10/3/13
 * Time: 2:20 PM
 * To change this template use File | Settings | File Templates.
 */

class WidgetFooter extends CWidget {
    public $catalogs, $parents;

    public function init() {
        $shows = MenuItem::model()->findAll(array(
            'select' => 'name,url,parent',
            'condition' => 'type = 0 and position = 1'
        ));
        $num = 0;
        $a = array();
        $n = array();
        $parents = array();
        foreach ($shows as $key => $item) {
            if ($item->parent == 1) {
                if (count($parents) < 10) {
                    array_push($parents, $item);
                }

            }
            if ($num == 21 || $key == 0 || $key == count($shows) - 1) {

                if (count($n) > 0) {

                    if ($key == count($shows) - 1) {
                        array_push($n, array('name' => $item->name, 'url' => $item->url));
                        array_push($a, array('sub' => $n));
                    } else {
                        array_push($a, array('sub' => $n));
                    }
                    $n = array();
                }
                $num = 1;
            }
            array_push($n, array('name' => $item->name, 'url' => $item->url));
            $num++;
        }
        $this->catalogs = $a;
        $this->parents = $parents;
    }

    public function run() {
        return CWidget::render('widgetFooter', array('results' => $this->catalogs, 'parents' => $this->parents));
    }
}